

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.FriendshipBeanRemote;

/**
 * Servlet implementation class ManageFriendshipServlet
 */
public class ManageFriendshipServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManageFriendshipServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String friend = request.getParameter("friend");
		String user = (String) request.getSession().getAttribute("userId");
		String outcome = request.getParameter("outcome");
		InitialContext context;
		FriendshipBeanRemote friendshipBean;
		
		if (friend != null) {;
			try {
				context = new InitialContext();
				Object ref = context.lookup("FriendshipBeanRemote");
				friendshipBean = (FriendshipBeanRemote)PortableRemoteObject.narrow(ref, FriendshipBeanRemote.class);
				
				if (outcome.equals("accepted")) {
					friendshipBean.establishFriendship(user, friend);
				}
				else {
					friendshipBean.deleteFriendshipRequest(user, friend);
				}
				response.sendRedirect("PendingFriendshipServlet");
			} catch (NamingException e) {
				System.out.println("Naming exception");
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
